import {
	useGetRefreshTokenStorage,
	useSetRefreshTokenStorage,
	useSetTokenStorage,
} from "./useStorage";


/**
 * 刷新token
 */
export const useRefreshToken = (param = {}) => {
	return new Promise(async (resolve, reject) => {
		// 发送登录请求
		await apiRefreshToken(param, {
				custom: {
					auth: false,
					loading: false
				},
				header: {
					Authorization: useGetRefreshTokenStorage(),
				},
			})
			.then((res) => {
				if (res.code === errorCode.success) {
					_saveToken(res);
					resolve(res);
				} else {
					resolve(res);
				}
			})
			.catch((err) => {
				reject(err);
			});
	});
};

export const _saveToken = (res) => {
	useSetTokenStorage(`${res.data.token_type} ${res.data.access_token}`)
	useSetRefreshTokenStorage(`${res.data.token_type} ${res.data.refresh_token}`)
};